using System;
using System.Diagnostics;
using NENAData.Errors;
using NENAData.LINQ;
using NENAData.NENAProvinces;
using NENAData.Transactions.Payments.PayPal;

public partial class PayPalEC : System.Web.UI.Page 
{

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            //Response.Write(Session["payment_amt"]);

        NVPAPICaller PayPalCaller = new NVPAPICaller();
        string retMsg = "";
        string token = "";

        Session["payment_amt"] = Request.QueryString["payment_amt"];
        Session["Affiliate_ID"] = Request.QueryString["Affiliate_ID"];
        Session["Member_ID"] = Request.QueryString["Member_ID"];
        Session["TransactionID_NENA"] = Request.QueryString["TransactionID_NENA"];
        Session["EventID"] = Request.QueryString["EventID"];
        Session["TransactionPaymentID"] = Request.QueryString["TransactionPaymentID"];

        if ( Session["payment_amt"] == null)
        {
            
            NENAAffiliate oNENAAffiliate  = new NENAAffiliate();
            Affiliate oAffiliate = new Affiliate();

            int intAffiliateID = Convert.ToInt32(Session["Affiliate_ID"]);
            oAffiliate = oNENAAffiliate.GetAffiliate_byAffiliateID(intAffiliateID);
            string strpay = string.Format("{0:00.00}", oAffiliate.Membership_Fee);
            Session["payment_amt"] = string.Format("{0:00.00}", oAffiliate.Membership_Fee);
        }

            string strAmount = Session["payment_amt"].ToString();

            Int32 intTransactionID_NENA = Convert.ToInt32(Session["TransactionID_NENA"]);
            string strReturnURL = string.Empty;
            string strFeeType = "";

            switch (intTransactionID_NENA)
            {
                case 8: //nena 2012 conference                //7 = nena 2011 conference
                    strReturnURL = "http://membership.nena.ca/paypal/EventConfirmation.aspx";
                    //strReturnURL = "http://localhost:3208/EventConfirmation.aspx";
                    strFeeType = "NENA Event Fees";
                    break;
                default: //all others
                    strReturnURL = "http://membership.nena.ca/paypal/OrderConfirmation.aspx";
                    strFeeType = "Membership Fees";
                    break;
            }
            
            bool boolReturn = PayPalCaller.ShortcutExpressCheckout(strAmount, strFeeType, ref token, ref retMsg, Convert.ToInt32(Session["Affiliate_ID"].ToString()), strReturnURL);
            if (boolReturn)
            {
                Session["token"] = token;
                PayPalTransactions transCheckout = new PayPalTransactions();

                switch (intTransactionID_NENA)
                {
                    case 7: //nena 2011 conference
                        break;
                    case 8: //nena 2012 conference
                        break;
                    default: //all others
                        transCheckout.NewTemplatePayPal(token, Session["payment_amt"].ToString(), Convert.ToInt32(Session["Member_ID"]), Convert.ToInt32(Session["Affiliate_ID"].ToString()), Convert.ToInt32(Session["TransactionID_NENA"].ToString()));
                        break;
                }

                Response.Redirect( retMsg, false );

            }
            else
            {
                Response.Redirect("APIError.aspx?" + retMsg);
            }
        }
        catch (Exception ex)
        {
            ErrHandler.WriteError(ex.InnerException + "<br />" + ex.Message + "<br />");
            Response.Write(ex.InnerException + "<br />" + ex.Message + "<br />");
        }
        finally
        {
            //_source.TraceEvent(TraceEventType.Stop, 0, "Stop PayPal Request");
        }
    }
}